package com.cloud.servlet;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.cloud.core.BaseServlet;
import com.cloud.dao.CaseBean;
import com.cloud.dao.PictureBean;
import com.cloud.service.CaseService;
import com.cloud.service.FileService;
import com.cloud.util.Code;
import com.cloud.util.CommUtil;

/**
 * 1Cloud1
 * @author Administrator
 * @since 2015-12-5
 * @version 1.0 
 */
@Controller
@RequestMapping("/case")
public class CaseServlet extends BaseServlet {
	@Autowired
	private CaseService CaseService;
	@Autowired
	private FileService FileService;
	
	/**
	 * 获取病例
	 * @param request
	 * @param patient
	 * @return
	 */
	@RequestMapping(value = "/{patient}/list", method = { RequestMethod.GET})
	public @ResponseBody Object list(HttpServletRequest request, @PathVariable String patient) {
		Map<String, Object> map = new HashMap<String, Object>();
		if(patient == null || CommUtil.isEmpty(patient)){
			map.put("code", Code.C400.key);
			map.put("msg", Code.C400.desc);
			return map;
		}
		CaseBean bean = new CaseBean();
		bean.setPat_id(patient);
		String type = request.getParameter("type");
		if(!CommUtil.isEmpty(patient))
			bean.setType(type);
		List<CaseBean> retCaseList = new ArrayList<CaseBean>();
		retCaseList = CaseService.getCaseList(bean);
		for(CaseBean cbean : retCaseList){
			for(PictureBean picture : cbean.getPictures()){
				if(!CommUtil.isEmpty(picture.getPicPath()))
					picture.setPicPath(FileService.getVisitUrl(picture.getPicPath()));
			}
		}
		map.put("code", Code.C200.key);
		map.put("msg", Code.C200.desc);
		map.put("data", retCaseList);
		return map;
	}
	
	/**
	 * 获取病例图片
	 * @param request
	 * @param caseId
	 * @return
	 */
	@RequestMapping(value = "/list/{caseId}/detail", method = { RequestMethod.GET})
	public @ResponseBody Object detail(HttpServletRequest request, @PathVariable String caseId) {
		Map<String, Object> map = new HashMap<String, Object>();
		if(caseId == null || (CommUtil.isEmpty(caseId))){
			map.put("code", Code.C400.key);
			map.put("msg", Code.C400.desc);
			return map;
		}
		List<PictureBean> retCaseDetailList = new ArrayList<PictureBean>();
		retCaseDetailList = CaseService.getCasePictureList(caseId);
		List<Map<String, String>> data = new ArrayList<Map<String, String>>();
		for(PictureBean picture : retCaseDetailList){
			Map<String, String> _map = new HashMap<String, String>();
			_map.put("key", picture.getPicId());
			_map.put("value", FileService.getVisitUrl(picture.getPicPath()));
			data.add(_map);
		}
		map.put("code", Code.C200.key);
		map.put("msg", Code.C200.desc);
		map.put("data", data);
		return map;
	}
	
	/**
	 * 添加病例
	 * @param request
	 * @param bean
	 * @return
	 */
	@RequestMapping(value = "/{patient}/add", method = { RequestMethod.POST})
	public @ResponseBody Object add(HttpServletRequest request, @PathVariable String patient, @RequestBody CaseBean bean) {
		Map<String, Object> map = new HashMap<String, Object>();
		if(bean == null || CommUtil.isEmpty(patient)){
			map.put("code", Code.C400.key);
			map.put("msg", Code.C400.desc);
			return map;
		}
		bean.setPat_id(patient);
		bean.setCase_id(CommUtil.getUUID()); //服务器生成病例ID
		if(CommUtil.isEmpty(bean.getType())){
			bean.setType("1");
		}
		CaseService.addCase(bean);
		map.put("code", Code.C200.key);
		map.put("msg", Code.C200.desc);
		map.put("data", bean.getCase_id());
		return map;
	}
	
	/**
	 * 修改病例
	 * @param request
	 * @param patient
	 * @param caseId
	 * @param bean
	 * @return
	 */
	@RequestMapping(value = "/{patient}/update/{caseId}", method = { RequestMethod.POST})
	public @ResponseBody Object update(HttpServletRequest request, @PathVariable String patient, @PathVariable String caseId, @RequestBody CaseBean bean) {
		Map<String, Object> map = new HashMap<String, Object>();
		if(bean == null || CommUtil.isEmpty(patient) || CommUtil.isEmpty(caseId)){
			map.put("code", Code.C400.key);
			map.put("msg", Code.C400.desc);
			return map;
		}
		bean.setCase_id(caseId);
		bean.setPat_id(patient);
		CaseService.updateCase(bean);
		map.put("code", Code.C200.key);
		map.put("msg", Code.C200.desc);
		map.put("data", bean.getCase_id());
		return map;
	}
	
	/**
	 * 删除病例
	 * @param request
	 * @param patient
	 * @param caseId
	 * @return
	 */
	@RequestMapping(value = "/{patient}/del/{caseId}", method = { RequestMethod.POST})
	public @ResponseBody Object delete(HttpServletRequest request, @PathVariable String patient, @PathVariable String caseId) {
		Map<String, Object> map = new HashMap<String, Object>();
		if(CommUtil.isEmpty(patient) || CommUtil.isEmpty(caseId)){
			map.put("code", Code.C400.key);
			map.put("msg", Code.C400.desc);
			return map;
		}
		List<PictureBean> retCaseDetailList = new ArrayList<PictureBean>();
		retCaseDetailList = CaseService.getCasePictureList(caseId);
		for(PictureBean picture : retCaseDetailList){
			FileService.deletePicture(picture);
		}
		CaseBean bean = new CaseBean();
		bean.setCase_id(caseId);
		bean.setPat_id(patient);
		CaseService.deleteCase(bean);
		map.put("code", Code.C200.key);
		map.put("msg", Code.C200.desc);
		map.put("data", bean.getCase_id());
		return map;
	}
}
